package com.qianyu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.SystemUser;
import com.qianyu.project.entity.WorkOrgPlan;
import com.qianyu.project.mapper.WorkOrgPlanMapper;
import com.qianyu.project.service.ISystemUserService;
import com.qianyu.project.service.IWorkOrgPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 施工日志明细 服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-01-30
 */
@Service
public class WorkOrgPlanServiceImpl extends ServiceImpl<WorkOrgPlanMapper, WorkOrgPlan> implements IWorkOrgPlanService {

    @Autowired
    private WorkOrgPlanMapper workOrgPlanMapper;
    @Autowired
    private ISystemUserService systemUserService;

    @Override
    public List<Map<String, Object>> getList(Map<String, String> map) {
        return workOrgPlanMapper.getList(map);
    }

    @Override
    public String getId() {
        String id=workOrgPlanMapper.getId();
        if(id == null||"".equals(id)){
            Calendar rightNow= Calendar.getInstance();
            Integer year = rightNow.get(Calendar.YEAR);
            Integer month = rightNow.get(Calendar.MONTH)+1;
            if(month<10){
                id=year+"0"+month+"0001";
            }else{
                id=year+month+"0001";
            }
        }else{
            id=(Long.parseLong(id)+1)+"";
        }
        return id;
    }

    @Transactional(rollbackFor = Exception.class)
    @FlowBegin("work_org_plan")
    @Override
    public int add(@FlowEntry("id") WorkOrgPlan workOrgPlan) {
        workOrgPlan.setApproveStatus(1);
        workOrgPlan.setFillUser(systemUserService.getOne(new QueryWrapper<SystemUser>().eq("user_realname",workOrgPlan.getFillUser())).getId().toString());
        save(workOrgPlan);
        return 1;
    }

    @Override
    public void updateWOP(WorkOrgPlan workOrgPlan) {
        workOrgPlan.setFillUser(systemUserService.getOne(new QueryWrapper<SystemUser>().eq("user_realname",workOrgPlan.getFillUser())).getId().toString());
        updateById(workOrgPlan);
    }
}
